<template>
	<om-addpage :scrollTop="scrollTop" style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		changeConfigAttr,
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				scrollTop:0,
				InstCacheKey:"pm-month-tmonthPerformanceEvaluate-inst-cache"
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods:{
			
			formSubmit(){
			let _that = this
			_that.om.formSave("tmonthPerformanceEvaluate/addTmonthPerformanceEvaluate",_that.formdata,_that)
			},
			
			onSelectGroup(item){
				let _that = this
				_that.$refs.autoform.clear("title_range","instId")
				_that.$refs.autoform.formdata.groupId = item.id
				_that.getDataList()
			},
			
			verifyInst(){
				let _that = this
				if(!_that.$refs.autoform.formdata.year){
					_that.om.toast("请先选择评价年份",3000)
					return false
				}else{
					return true
				}
			},
			
			getDataList(){
				let _that = this
				_that.om.request(
					"tmonthPerformanceEvaluate/getDataList",
					{
						year: _that.$refs.autoform.formdata.year,
						monthCode: _that.$refs.autoform.formdata.monthCode,
						groupId:_that.$refs.autoform.formdata.groupId,
						instId:_that.$refs.autoform.formdata.instId
					}
				).then(res=>{
					errorModal(_that, '提示信息', " 加载了" + (res.value.length || 0 )+ "条信息")
					_that.subEditorObj["scoreInfo"].workList = res.value
					_that.$refs.autoform.changeConfigAttr("scoreInfo", "length", res.value.length || 0)
				})
			},
			
			onSelectMonth(item){
				let _that = this
				_that.$refs.autoform.formdata.monthCode = item.code
				_that.getDataList()
			},
			
			onSelectZpos(item){
				let _that = this
				_that.$refs.autoform.formdata.zinstId = item.instId
				_that.$refs.autoform.formdata.zinstName = item.instName
			},
			getMonthList(){
				let _that = this
				let monthList = [{text:"请选择"}]
				_that.om.request(
					"dictZero/getListByCode",
					{
						code:"35803"
					}
				).then(res=>{
					if(_that.$refs.autoform.formdata.evaluateItemCatCode !== "37676"){
						for(let item of res){
							let obj =  {
								attr:{},
								text:"",
								value:"",
							}
							obj.attr = item
							obj.text = item.dicname
							obj.value = item.code
							monthList.push(obj)
						}
						_that.$refs.autoform.configData[0].editors[3].prompt = '请选择'
						_that.$refs.autoform.configData[0].editors[3].data = monthList
						_that.$refs.autoform.configData[0].editors[3].type ="selectbox"
						_that.$refs.autoform.configData[0].editors[3].onSelect ="onSelectMonth"
					}else{
						_that.onSelectMonth()
					}
				})
			},
			getGroupList(){
				let _that = this
				let groupList = [{text:"请选择"}]
				_that.om.request(
					"performancePowerDict/getGroupCombobox"
				).then(res=>{
					for(let item of res){
						let obj =  {
							attr:{},
							text:"",
							value:"",
						}
						obj.attr = item
						obj.text = item.groupName
						obj.value = item.id
						groupList.push(obj)
					}
					_that.$refs.autoform.configData[1].editors[0].prompt = '请选择'
					_that.$refs.autoform.configData[1].editors[0].data = groupList
					_that.$refs.autoform.configData[1].editors[0].type ="selectbox"
					_that.$refs.autoform.configData[1].editors[0].onSelect ="onSelectGroup"
				})
			},
			getInstList(){
				let _that = this
				_that.$refs.autoform.changeEditor("title_range","instId",{
					link: "../../public/pageSearch/pageSearch",
					"linkParam": {
						other: JSON.stringify({
						}),
						method: "post",
						namefield: 'instName',
						searchfield: "instName",
						initload: true,
						noLoadMore:true,
						url: 'tinstitution/findNotBoard1',
						cachename: _that.InstCacheKey,
						placeholder: "请输入部门名称"
					}
				})
			},
			onSelectYear(item){
				let _that  = this
				_that.getMonthList()
				_that.getGroupList()
				_that.getInstList()
			},
			
			initLoad(options){
				let _that = this
				let ajaxArr = [
					urlUtil.zposUrl(options.code),
					urlUtil.dictZeroComboboxUrlByInCode("37674","37675")
				]
				formModelLoad(ajaxArr,function(res,mainData){
					changeEditor("topinfo","zposId","data",configSelectData(res[0],"id","posname"),mainData)
					changeEditor("topinfo","evaluateItemCatCode","data",configSelectData(res[1],"code","dicname"),mainData)
					let date = new Date()
					let prevYear = Number(date .getFullYear())-1
					let nextYear = Number(date .getFullYear())
					let yearList = [{id:prevYear,dicname:prevYear},{id:nextYear,dicname:nextYear}]
					changeEditor("topinfo","year","data",configSelectData(yearList,"id","dicname"),mainData)
					changeConfigAttr("scoreInfo", "showLength", true, mainData)
					if(_that.subEditorObj["scoreInfo"]){
						changeEditor("scoreInfo","objStr1",{
							showAdd: false,
							showDel: false,
							showArrow: false,
							length: 0,
							showLength: true,
							titleTemplate:"<%=userName || '未填写'%>|<%= posName || '未填写'%>|<%= instName || '未填写'%>",
							subtitle1Template:"<%=indexScore || '未填写'%>|<%= indexWeight|| '未填写' %>|<%= workScore || '未填写'%>|<%=workWeight || '未填写'%>",
							subtitle2Template:"<%=subjectiveScore || '未填写'%>|<%= subjectiveWeight || '未填写'%>|<%= customScore || '未填写'%>|<%= customWeight || '未填写'%> ",
							subtitle3Template:"<%=integralScore || '未填写'%>|<%= integralWeight || '未填写'%>|<%= performanceScore || '未填写'%>|<%= resultCode || '未填写'%>"
						},null,mainData)
					}
				},options)
			}
		},
		onLoad(options) {
			let _that = this
			_that.initLoad(options)
		},
		onPageScroll(e) {
			if(e.scrollTop>100){
			    this.scrollTop = e.scrollTop
			}else{
				this.scrollTop = 0
			}
		},
		onShow() {
			let _that  = this
			if(_that.$refs.autoform){
				uni.getStorage({
					key:_that.InstCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.instId = d.id
							formdata.instName = d.instName
							_that.$refs.autoform.clear("title_range","groupId")
							_that.getDataList()
							uni.removeStorage({
								key:_that.InstCacheKey
							})
						}
					}
				})
			}
		}
	}
</script>

<style></style>                                                                                                                                                                                                                                                                                                                                      
